package ism.api{
	public class AppFunctions{
	
	import mx.managers.SystemManager;
        	
        	import mx.controls.TextArea;
        	import mx.containers.TitleWindow;
        	import mx.managers.PopUpManager;
        	import mx.core.UITextField;
        	import mx.controls.Text;
            import flash.net.navigateToURL;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.events.ItemClickEvent;    
	        import mx.managers.PopUpManager;
      		import mx.containers.TitleWindow;
            import flash.geom.Point;

			[Bindable] public var catChoice:Array;                          
            [Bindable] private var photoFeed:XML;            
            [Bindable] private var serviceDocument:XML;
            [Bindable] private var serviceDocumentList:XMLList;
			[Bindable] private var feedDocument:XML;

            private function setHomePage(event:ResultEvent):void          {
				trace ("Hello world!");
				entryEditor.htmlWidget.htmlText = event.result as String;
            }

            private function startResultHandler(event:ResultEvent):void {
				var tmp:XML = event.result as XML;
            }

            // Result handler - gets called after RSS is loaded.
            private function photoResultHandler(event:ResultEvent):void {            	
            	trace("photoResultHandler(event) Called ");
            	namespace local = "http://www.w3.org/2007/app";
            	namespace atom = "http://www.w3.org/2005/Atom"
            	use namespace local;
            	use namespace atom;
            
				var tmp:XML = event.result as XML;
				trace(tmp);
				serviceDocument = event.result as XML;

				feedPanel.entriesRepeater.dataProvider = null;
				
				serviceDocumentList = tmp.workspace;
				
				trace("workspaces :" + serviceDocumentList);
            }
            
           	private function feedResultHandler(event:ResultEvent):void {            	
            	trace("feedResultHandler(event) called" );
            	namespace local = "http://www.w3.org/2007/app";
            	namespace atom = "http://www.w3.org/2005/Atom"
            	use namespace local;
            	use namespace atom;
            
				var tmp:XML = event.result as XML;
				feedDocument = event.result as XML;
				feedPanel.entriesRepeater.dataProvider = feedDocument.entry; 
            }
            
     
            private function viewCollections(event:XML):void{	
				trace("viewCollections(event)");
				
				namespace local = "http://www.w3.org/2007/app";
            	namespace atom = "http://www.w3.org/2005/Atom"
            	use namespace local;
            	use namespace atom;
            
				var tmp:XML = event;			
				
				var urlRoot:String = atomPubService.rootURL;
			    var urlRoot2:String = feedFetchService.rootURL;
				
				//photoService.lastResult
				
				trace("root path :" + urlRoot);
				trace("tmp: " + tmp);
				feedFetchService.url = tmp.@href;
				if(feedFetchService.url == null){
					Alert.show("Using URL " + urlInput.text, "Could not dereference Feed");
					return;			
				}
			
				trace("URL :" + feedFetchService.url);
				trace("Fetching the service.");
				feedFetchService.send();
            }
                       
            private function fetchService(oEvent:Object):void{           
            	feedPanel.entriesRepeater.dataProvider = null;
            	collectionRepeater.dataProvider = null;
				//collectionChoice.dataProvider = null;

            	atomPubService.url = urlInput.text;
				atomPubService.send();
            }    
            
            private function displayHelp(oEvent:Object):void{           
            	var viewer:XmlViewer = XmlViewer(PopUpManager.createPopUp( this, XmlViewer , true));
				viewer.xmlText.text = serviceDocument.toXMLString();
				
				mx.managers.PopUpManager.centerPopUp(viewer);
            }
            
			private function viewSource(e:MouseEvent):void {
					if(e.target is mx.core.UITextField){
 					var viewer:XmlViewer = XmlViewer(PopUpManager.createPopUp( this, XmlViewer , true));
					
					if(serviceDocument == null){
					   viewer.xmlText.text = "You must first fetch an AtomPub ServiceDocument.";
					}else{
      					viewer.xmlText.text = serviceDocument.toXMLString();
					}
					
					//viewer.autoLayout = true;
					mx.managers.PopUpManager.centerPopUp(viewer);									
					//mx.managers.SystemManager.
				}
   			}  
   			
   			public static function viewXml(e:Event, xml:Object):void {
   					trace("viewXml ");
					trace(" current target = " + e.currentTarget);
   		 			trace(" current target = " + e.target);   				
   					if(e.target is mx.core.UITextField){
   										
 					var viewer:XmlViewer = XmlViewer(PopUpManager.createPopUp( this, XmlViewer , true));
					if(xml == null){
					   viewer.xmlText.text = "You must first fetch an AtomPub ServiceDocument.";
					}else{
      					viewer.xmlText.text = xml.toString();
					}
										
					mx.managers.PopUpManager.centerPopUp(viewer);									
   					}
	   			}  
   			
   			
           private function feedFaultHandler(event:FaultEvent):void{
                Alert.show("Using URL " + feedFetchService.url, "Could not dereference the Feed");
            }
            
            private function serviceFaultHandler(event:FaultEvent):void{
                Alert.show("Using URL " + urlInput.text, "Could not dereference APP Service Document");
            }
                        
            private function viewEntry(entryXml:XML):void{
            	trace("viewEntry(entryXml)");
           	    trace("HTML : " + entryXml);
            	entryEditor.htmlWidget.htmlText = entryXml.toString();            	
            }
            
            private function viewCollection(xml:XML):void{
            	feedPanel.entriesRepeater.dataProvider = null;             	          		
            	collectionRepeater.dataProvider =  xml.collection;
            	
            	
            	namespace local = "http://www.w3.org/2007/app";
            	namespace atom = "http://www.w3.org/2005/Atom"
            	use namespace local;
            	use namespace atom;
            	
            	var colXml:XMLList = xml.collection;
            	trace(colXml);
	            var choice:Array = new Array(colXml.length());
	            var item:XML;
	            var test:List = new List();
	            var i:int = 0;
	            
	            for each(item in colXml) {
    	            trace("item: " + item.toXMLString());
    	        	var label:Object = new Object();
    	        	label.text = item.title;
    	        	label.label = item.title;
    	        	label.data = colXml;
					label.toolTip = colXml;
					label.id = item.title;
    	        	
					choice[i] = label;
					i++;
        	    }	
            	
            	//collectionChoice.dataProvider = choice;
            	catChoice = choice;
            }
	
	}
}